package com.example.football;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.football.entity.Peopleliketeam;
import com.example.football.entity.Schedule;
import com.example.football.util.JDBCUtils;
import com.example.football.util.SetTeamlogo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class PeopleliketeamActivity extends AppCompatActivity {

    ListView myList;
    static public Integer peopleid ;
    static public String account;
    static public String name;
    static public ArrayList<Peopleliketeam> list;

    EditText et_peopleliketeam;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_peopleliketeam);

        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }

        peopleid = Main2Activity.peopleid;
        account = Main2Activity.account;
        name = Main2Activity.name;
        list= new ArrayList<>();

        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("id",peopleid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url="http://"+ JDBCUtils.ip +":9000/APPpeopleliketeam";
        RequestQueue requestQueue= Volley.newRequestQueue(PeopleliketeamActivity.this);
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                try {
                    Log.d("信息", jsonObject.toString());
                    String msg = jsonObject.getString("msg");
                    Log.d("msg", msg);

                    JSONArray detail = jsonObject.getJSONArray("detail");
                    for(int i = 0; i < detail.length(); i++){
                        JSONObject data = detail.getJSONObject(i);
                        list.add(new Peopleliketeam(data.getInt("id"),data.getInt("peopleid"),data.getString("liketeam")));
                    }
                    myList = findViewById(R.id.mylist3);
                    final MyAdapter myAdapter = new MyAdapter();
                    myList.setAdapter(myAdapter);
                    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                            String str=list.get(position).getLiketeam();
                            Log.d("检查：","OnItemClick功能实现！\n"+str);
                        }
                    });
                    myAdapter.setOnItemDeleteClickListener(new onItemDeleteListener() {
                        @Override
                        public void onDeleteClick(int i) {
                            deletepeopleliketeam(peopleid,list.get(i).getLiketeam());
                            Toast.makeText(PeopleliketeamActivity.this, "delete:" +list.get(i).getLiketeam(), Toast.LENGTH_SHORT).show();
                            myAdapter.notifyDataSetChanged();
                        }
                    });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(PeopleliketeamActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {//获取item的数量
            return list.size();
        }

        @Override
        public Object getItem(int i) {//i表示当前item的序号，序号从0开始编写，getIte用于获取某个item
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {//获取选中的item的编号
            return i;
        }

        @Override//用于配置listview要加载的内容，包括视图和数据
        public View getView(final int i, View view, ViewGroup viewGroup) {

//             这种写法，每次调用都要生成一个新的View,那么当数据多了，那就要生成很多个view，
//             这样会造成资源浪费
//             View view1 = View.inflate(MainActivity.this,R.layout.itemlayout,null);
//             title1 = view1.findViewById(R.id.title1);
//             iv = view1.findViewById(R.id.iv);
//             price = view1.findViewById(R.id.price);
//             title1.setText(foods.get(i).getTitle());
//             iv.setBackgroundResource(foods.get(i).getIcon());
//             price.setText(foods.get(i).getPrice());


//             优化写法
            ViewHolder holder = null;
            if(view == null){
                //这里是将item视图与view绑定起来
                view = View.inflate(PeopleliketeamActivity.this,R.layout.peopleliketeam_item,null);
                holder = new ViewHolder();
//                注意，这里是去view的视图里面拿id
                holder.team = view.findViewById(R.id.team);
                holder.logo = view.findViewById(R.id.logo);
                holder.shanchu = view.findViewById(R.id.shanchu);

                holder.team.setText(list.get(i).getLiketeam());
//                holder.iv.setBackgroundResource(foods.get(i).getIcon());

                SetTeamlogo.setTeamlogo(holder.logo,list.get(i).getLiketeam());

                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
                holder.team.setText(list.get(i).getLiketeam());
//                holder.iv.setBackgroundResource(foods.get(i).getIcon());

                SetTeamlogo.setTeamlogo(holder.logo,list.get(i).getLiketeam());
            }
            holder.shanchu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view1) {
                    mOnItemDeleteListener.onDeleteClick(i);
                }
            });
            return view;
        }



        private onItemDeleteListener mOnItemDeleteListener;

        public void setOnItemDeleteClickListener(onItemDeleteListener mOnItemDeleteListener) {
            this.mOnItemDeleteListener = mOnItemDeleteListener;
        }
    }

    public interface onItemDeleteListener {
        void onDeleteClick(int i);
    }

    static class ViewHolder{
        TextView team;
        ImageView logo;
        ImageView shanchu;
    }

    public  void deletepeopleliketeam(int peopleid,String liketeam){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("peopleid",peopleid);
            jsonObject.put("liketeam",liketeam);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url="http://"+ JDBCUtils.ip +":9000/APPdeletepeopleliketeam";
        RequestQueue requestQueue= Volley.newRequestQueue(PeopleliketeamActivity.this);
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    Log.d("删除信息", jsonObject.toString());
                    String msg = jsonObject.getString("msg");
                    if(msg.equals("删除成功")){
                        finish();
                        Intent intent=new Intent(PeopleliketeamActivity.this,PeopleliketeamActivity.class);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(PeopleliketeamActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.peopleliketeam_iv_back:
                finish();
                break;
            case  R.id.iv_search:
                et_peopleliketeam = findViewById(R.id.et_peopleliketeam);
                String liketeam = et_peopleliketeam.getText().toString().trim();
                JSONObject jsonObject=new JSONObject();
                try {
                    jsonObject.put("peopleid",peopleid);
                    jsonObject.put("liketeam",liketeam);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String url="http://"+ JDBCUtils.ip +":9000/APPinsertpeopleliketeam";
                RequestQueue requestQueue= Volley.newRequestQueue(PeopleliketeamActivity.this);
                JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            Log.d("添加信息", jsonObject.toString());
                            String msg = jsonObject.getString("msg");
                            if(msg.equals("插入成功")){
                                finish();
                                Intent intent=new Intent(PeopleliketeamActivity.this,PeopleliketeamActivity.class);
                                startActivity(intent);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(PeopleliketeamActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                    }
                });
                requestQueue.add(jsonObjectRequest);
                break;
        }
    }
}
